package com.poisonh.unifiedauth.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.adapter.InfoCenterAdapter;
import com.poisonh.unifiedauth.model.InfoCenterModel;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.GsonUtils;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息中心
 * Created by PoisonH on 2016/7/11.
 */
public class InfoCenterActivity extends BaseActivity implements View.OnClickListener, InfoCenterAdapter.OnItemClickLitener
{
    private XRecyclerView mXRecyclerView;
    private InfoCenterAdapter mInfoCenterAdapter;
    private int page = 1;
    private List<InfoCenterModel> mList;
    private boolean isLoad = false;

    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_infocenter);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        mList = new ArrayList<>();
        setTitleTxt("消息中心");
        mRightBtn.setVisibility(View.GONE);
        mLeftBtn.setOnClickListener(this);
        mXRecyclerView = (XRecyclerView) findViewById(R.id.rv_infocenter);
        LinearLayoutManager mManager = new LinearLayoutManager(this);
        mManager.setOrientation(LinearLayoutManager.VERTICAL);
        mXRecyclerView.setLayoutManager(mManager);
        mInfoCenterAdapter = new InfoCenterAdapter(this);
        mXRecyclerView.setAdapter(mInfoCenterAdapter);
        mInfoCenterAdapter.setOnItemClickLitener(this);
        mXRecyclerView.setPullRefreshEnabled(true);
        mXRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener()
        {
            @Override
            public void onRefresh()
            {
                page = 1;
                isLoad = false;
                getInfoCenterData(page);
            }

            @Override
            public void onLoadMore()
            {
                page += 1;
                isLoad = true;
                getInfoCenterData(page);
            }
        });
        getInfoCenterData(page);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ib_left_btn:
                finish();
                break;
        }
    }

    /**
     * 获取小心中心数据
     *
     * @param page
     */
    private void getInfoCenterData(int page)
    {
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("page", page + "");
        params.put("size", "10");
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.GETMESSLIST, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                Message msg = Message.obtain();
                msg.obj = response;
                mHandler.sendMessage(msg);
                mXRecyclerView.loadMoreComplete();
                mXRecyclerView.refreshComplete();
            }

            @Override
            public void onFailure(String failReason)
            {
                mXRecyclerView.loadMoreComplete();
                mXRecyclerView.refreshComplete();
            }
        });
    }

    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            mList = GsonUtils.parseInfoCenterData((String) msg.obj);
            if (isLoad)
            {
                if (mList.size() < 0)
                {
                    mXRecyclerView.loadMoreComplete();
                    ToastUtils.showToast(InfoCenterActivity.this, "没有更多数据");
                    mXRecyclerView.setLoadingMoreEnabled(false);
                } else
                {
                    mInfoCenterAdapter.setData(mList);
                }
            } else
            {
                mInfoCenterAdapter.updateData(mList);
            }
        }
    };

    /**
     * 单击事件处理
     *
     * @param view
     * @param position
     */
    @Override
    public void onItemClick(View view, int position)
    {
        Intent mIntent = new Intent(InfoCenterActivity.this, InfoDetailsActivity.class);
        mIntent.putExtra("INFOID", mInfoCenterAdapter.getList().get(position).getMESS_ID());
        startActivity(mIntent);
    }
}
